package collections;

import java.util.*;

class Set_HashSetTest {
    /**
     * 
     * @Title: main  
     * @Description: Set遍历的方法也是三种：1. for循环(但是只能使用增强for) 2. 迭代器遍历  3. 先转换成数组再遍历数组。 
     *
     * @param args
     */
    public static void main(String[] args) {
        HashSet<String> hashSet = new HashSet<String>();
        hashSet.add("苹果");
        hashSet.add("谷歌");
        hashSet.add("微软");
        hashSet.add("MOTO");

        System.out.println("增强for遍历HashSet：");
        for (String string : hashSet) {
            System.out.println(string);
        }

        System.out.println("迭代器遍历HashSet：");
        Iterator<?> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        System.out.println("先转换成数组在遍历HashSet：");
        Object[] stringArray = hashSet.toArray();
        for (Object object : stringArray) {
            System.out.println(object);
        }
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println(stringArray[i]);
        }
    }
}
